/**
 * 智慧粮仓 - 能耗监测模块模拟数据
 * 提供模拟的电力数据，用于开发和测试
 */

/**
 * 生成随机数
 * @param {number} min 最小值
 * @param {number} max 最大值
 * @param {number} decimals 小数位数
 * @returns {number} 随机数
 */
function getRandomNumber(min, max, decimals = 2) {
    const random = Math.random() * (max - min) + min;
    return parseFloat(random.toFixed(decimals));
}

/**
 * 生成模拟电力数据
 * @returns {Array} 电力数据数组
 */
function generateMockPowerData() {
    // 电压数据
    const voltageData = [
        { id: 1, name: 'A相电压', type: 'VOLTAGE', value: getRandomNumber(220, 235), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 2, name: 'B相电压', type: 'VOLTAGE', value: getRandomNumber(220, 235), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 3, name: 'C相电压', type: 'VOLTAGE', value: getRandomNumber(220, 235), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 4, name: 'A-B线电压', type: 'VOLTAGE', value: getRandomNumber(375, 395), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 5, name: 'B-C线电压', type: 'VOLTAGE', value: getRandomNumber(375, 395), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 6, name: 'C-A线电压', type: 'VOLTAGE', value: getRandomNumber(375, 395), status: 'NORMAL', time: formatTime(new Date()) }
    ];
    
    // 电流数据
    const currentData = [
        { id: 7, name: 'A相电流', type: 'CURRENT', value: getRandomNumber(0.1, 0.6), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 8, name: 'B相电流', type: 'CURRENT', value: getRandomNumber(0.1, 0.9), status: generateRandomStatus(), time: formatTime(new Date()) },
        { id: 9, name: 'C相电流', type: 'CURRENT', value: getRandomNumber(0.1, 0.7), status: 'NORMAL', time: formatTime(new Date()) }
    ];
    
    // 功率数据
    const powerData = [
        { id: 10, name: '有功功率', type: 'POWER', value: getRandomNumber(0.5, 3.0), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 11, name: '无功功率', type: 'POWER', value: getRandomNumber(0.2, 1.0), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 12, name: '视在功率', type: 'POWER', value: getRandomNumber(0.6, 3.2), status: 'NORMAL', time: formatTime(new Date()) }
    ];
    
    // 电能数据
    const energyData = [
        { id: 13, name: '有功电能', type: 'POWER', value: getRandomNumber(45, 60), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 14, name: '无功电能', type: 'POWER', value: getRandomNumber(10, 20), status: 'NORMAL', time: formatTime(new Date()) }
    ];
    
    // 其他参数
    const otherData = [
        { id: 15, name: '频率', type: 'FREQUENCY', value: getRandomNumber(49.8, 50.2, 1), status: 'NORMAL', time: formatTime(new Date()) },
        { id: 16, name: '功率因数', type: 'POWER_FACTOR', value: getRandomNumber(0.85, 0.95), status: 'NORMAL', time: formatTime(new Date()) }
    ];
    
    // 合并所有数据
    return [...voltageData, ...currentData, ...powerData, ...energyData, ...otherData];
}

/**
 * 生成随机状态
 * @returns {string} 状态字符串: NORMAL, WARNING, ALARM
 */
function generateRandomStatus() {
    const random = Math.random();
    if (random < 0.7) {
        return 'NORMAL';
    } else if (random < 0.9) {
        return 'WARNING';
    } else {
        return 'ALARM';
    }
}

/**
 * 格式化时间为 HH:MM:SS
 * @param {Date} date 日期对象
 * @returns {string} 格式化的时间字符串
 */
function formatTime(date) {
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${hours}:${minutes}:${seconds}`;
}

// 导出函数
window.generateMockPowerData = generateMockPowerData; 